package com.hellpro.lolbox.video.presenter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.utils.UIManger;
import com.hellpro.lolbox.video.bean.VideoClassifyBean;
import com.hellpro.lolbox.video.bean.VideoHotBean;
import com.hellpro.lolbox.video.bean.VideoManitoBean;
import com.hellpro.lolbox.video.bean.VideoSelectionBean;
import com.hellpro.lolbox.video.bean.VideoSelectionVpBean;
import com.hellpro.lolbox.video.model.IVideoInfo;
import com.hellpro.lolbox.video.model.LoadClassifyInfo;
import com.hellpro.lolbox.video.model.LoadManitoInfo;
import com.hellpro.lolbox.video.model.LoadVideoHotInfo;
import com.hellpro.lolbox.video.model.LoadVideoSelectionInfo;
import com.hellpro.lolbox.video.model.LoadVideoSelectionVpInfo;
import com.hellpro.lolbox.video.model.ManitoBiz;
import com.hellpro.lolbox.video.model.VideoBiz;
import com.hellpro.lolbox.video.model.VideoClassifyBiz;
import com.hellpro.lolbox.video.model.VideoHotLvAdapter;
import com.hellpro.lolbox.video.model.VideoSelectionLvAdapter;
import com.hellpro.lolbox.video.model.VideoSelectionVpAdapter;
import com.hellpro.lolbox.video.view.IVideoView;
import com.hellpro.lolbox.video.view.IVideoWebActivity;
import com.hellpro.lolbox.video.view.VideoWebActivity;
import com.hellpro.lolbox.video.view.fragment.IClassifyFragment;
import com.hellpro.lolbox.video.view.fragment.IManitoFragment;
import com.hellpro.lolbox.video.view.fragment.IVideoHotFragment;
import com.hellpro.lolbox.video.view.fragment.IVideoSelectionFragmet;

import java.util.List;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/18
 * 备注:
 */

public class VideoPresenter {

    private IVideoView mIVideoView;
    private IVideoInfo mIVideoInfo;
    private VideoBiz mVideoBiz;
    private IClassifyFragment mICalssifyFragment;
    private IManitoFragment mIManitoFragment;
    private VideoClassifyBiz mClassifyBiz;
    private Context mContext;
    private ManitoBiz manitoBiz;
    private Handler mHandler;
    private IVideoWebActivity mIVideoWebActivity;
    private IVideoHotFragment mIVideoHotFragment;
    private IVideoSelectionFragmet mIVideoSelectionFragmet;

    public VideoPresenter(IVideoView iVideoView){
        mVideoBiz = new VideoBiz();
        this.mIVideoView = iVideoView;
    }
    public VideoPresenter(IVideoInfo iVideoInfo){
        mVideoBiz = new VideoBiz();
        this.mIVideoInfo = iVideoInfo;
    }
    public VideoPresenter(IClassifyFragment iCalssifyFragment,Context context){
        mClassifyBiz = new VideoClassifyBiz();
        this.mICalssifyFragment = iCalssifyFragment;
        this.mContext = context;
    }
    public VideoPresenter(IManitoFragment iManitoFragment, Context context, Handler handler){
        manitoBiz = new ManitoBiz();
        this.mIManitoFragment = iManitoFragment;
        this.mContext = context;
        this.mHandler = handler;
    }

    public VideoPresenter(IVideoHotFragment iVideoHotFragment, Context context,Handler handler){
        this.mIVideoHotFragment = iVideoHotFragment;
        this.mContext = context;
        this.mHandler = handler;
    }

    public VideoPresenter(IVideoWebActivity iManitoFragment){
        this.mIVideoWebActivity = iManitoFragment;
    }

    public VideoPresenter(IVideoSelectionFragmet iVideoSelectionFragmet,Handler handler){
        this.mIVideoSelectionFragmet = iVideoSelectionFragmet;
        this.mHandler = handler;
    }

    /**
     *
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/19
     * 备注:设置Video主页面的ViewPager的适配器
     */

    public void setViewPagerAdapter(){
        mVideoBiz.setViewpager(mIVideoView.getVideoFragmentManager(),mIVideoView.getTitles(),mIVideoView.getContentFragments(),mIVideoView.getContentViewPager());
    }
    /**
     *
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/19
     * 备注:设置分类导航页面的RecyclerView的适配器
     */
    public void setClassifyAdapter(List<VideoClassifyBean> data, ListView mLv, Context context){
        mClassifyBiz.setClassifyRecyclerAdapter(data,mLv,context);
    }

    //下载分类导航的数据并解析
    public void setClassifyInfo(){
        new LoadClassifyInfo(mICalssifyFragment.getClassifyUrl(),mContext).startLoadInfo();
    }

    //下载大神直播间的数据并解析
    public void setMantioInfo(){
        new LoadManitoInfo(mIManitoFragment.getManitoUrl(),mContext,mHandler).startLoadManitoInfo();
    }
    //设置大神直播间的ListView的适配器
    public void setManitoAdapter(List<VideoManitoBean> data, PullToRefreshListView mLv, Context context){
        manitoBiz.setVideoRefreshLvAdapter(data,mLv,context);
    }
    //设置大神直播间的ListView的监听
    public void setManitoLiatener(List<VideoManitoBean> data, AdapterView<?> parent, View view, int position){
        VideoManitoBean videoManitoBean = data.get(position);
        String url = videoManitoBean.getUrl();
        UIManger.startToWebActivity(parent.getContext(), VideoWebActivity.class,url);
    }
    //设置Video页面的二级页面
    public void setVideoWebView(){
        String url = mIVideoWebActivity.getUrl();
        WebView webView = mIVideoWebActivity.getWebView();
        WebSettings settings = webView.getSettings();
        settings.setDefaultTextEncodingName("UTF-8");
        //设置webView的网址
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            //修改默认的浏览器
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    //设置Video的热门的信息
    public void setVideoHotInfo(){
        new LoadVideoHotInfo(mIVideoHotFragment.getUrl(),mContext,mHandler).startLoadHotInfo();
    }
    //设置Video的热门的ListView的适配器
    public void setVideoHotAdapter(final List<VideoHotBean> data, final PullToRefreshListView mLv, final Context context){
        VideoHotLvAdapter videoHotLvAdapter = new VideoHotLvAdapter();
        videoHotLvAdapter.setData(data);
        mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        mLv.setAdapter(videoHotLvAdapter);
        mLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(context, "没有更多数据", Toast.LENGTH_SHORT).show();
                //设置下拉刷新的延迟时间，时间太短刷新图标无法隐藏
               /* mLv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mLv.onRefreshComplete();
                    }
                }, 1000);*/
            }
        });

    }

    /**
     *
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/20
     * 备注:精选页面的设置适配器
     *
     */

    public void setVideoSelectionLvAdapter(List<VideoSelectionBean> data, final Context context){
        final PullToRefreshListView mLv = mIVideoSelectionFragmet.getRefreshListView();
        //设置头部视图
        LinearLayout view = mIVideoSelectionFragmet.getLvHeaderView();
        mLv.getRefreshableView().addHeaderView(view);
        VideoSelectionLvAdapter adapter = new VideoSelectionLvAdapter();
        adapter.setData(data);
        mLv.setAdapter(adapter);

        mLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(context, "没有更多数据", Toast.LENGTH_SHORT).show();
                //设置下拉刷新的延迟时间，时间太短刷新图标无法隐藏
                mLv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mLv.onRefreshComplete();
                    }
                }, 1000);
            }
        });
    }
    public void setVideoSelectionVp(final List<VideoSelectionVpBean> data){
        new VideoSelectionVpAdapter(data,mIVideoSelectionFragmet.getBanner()).setVpAdapter();
    }

    //设精选页面的ViewPager数据
    public void setVideoSelectionVpInfo(){
        new LoadVideoSelectionVpInfo(mIVideoSelectionFragmet.getVpUrl(),mContext,mHandler).startLoadSelectionVpInfo();
    }
    //设精选页面的ListView数据
    public void setVideoSelectionInfo(){
        new LoadVideoSelectionInfo(mIVideoSelectionFragmet.getUrl(),mContext,mHandler).startLoadSelectionInfo();
    }

}
